package br.com.ianalise.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.Locale;

import javax.swing.AbstractAction;
import javax.swing.AbstractButton;
import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

import br.com.ianalise.negocio.excecoes.IAnaliseException;
import br.com.ianalise.negocio.fachada.Fachada;
import br.com.ianalise.negocio.pojo.Paciente;
import br.com.ianalise.outros.Validacoes;

public class CadastrarPaciente extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1828324002739453911L;
	private JPanel contentPane;
	private JTextField textFieldPaciente;
	private JTextField textFieldRG;
	private JTextField textFieldOrgaoExpedidor;
	private JTextField textFieldNomeDoPai;
	private JTextField textFieldNomeDaMae;
	private JFormattedTextField formattedTextFieldTelefone;
	private JTextField textFieldEmail;
	private JFormattedTextField formattedTextFieldCelular;
	private JTextField textFieldConvenio;
	private JTextField textFieldMedico;
	private JTextField textFieldProfissao;
	private JTextField textFieldRua;
	private JTextField textFieldNumero;
	private JTextField textFieldBairro;
	private JTextField textFieldCidade;
	private JTextField textFieldComplemento;
	private JTextArea textAreaObservacoes;
	protected String tempTextAreaObservacoes;
	protected int contReleased;
	private JButton btnCadastrar;
	private AbstractButton btnCancelar;
	private JButton btnLimparFormulrio;
	private JComboBox<Object> comboBoxSexo;
	private JFormattedTextField formattedTextFieldNascimento;
	private JComboBox<Object> comboBoxNaturalidade;
	private JComboBox<Object> comboBoxEstadoCivil;
	private JFormattedTextField formattedTextFieldCPF;
	private JComboBox<Object> comboBoxEstadoRG;
	private JFormattedTextField formattedTextFieldEmissaoRG;
	private JComboBox<Object> comboBoxOrigem;
	private JComboBox<Object> comboBoxEstadoEndereco;
	private JFormattedTextField formattedTextFieldCEP;

	/**
	 * Create the frame.
	 * @throws IAnaliseException 
	 */
	public CadastrarPaciente(final String titulo, String subTitulo) throws IAnaliseException {
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				btnCancelar.doClick();
			}
		});
		setModal(true);
		this.setSize(800, 530);
		this.setLocationRelativeTo(null);
		setResizable(false);
		setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
		//setBounds(100, 100, 800, 530);
		this.setTitle(titulo + " - " + subTitulo);
		contentPane = new JPanel();
		
		/*
		 * Quando apetar ESC em qualquer lugar do contentPane executar o botão cancelar
		 */
		contentPane.getInputMap(JPanel.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, true), "CANCELAR");
		contentPane.getActionMap().put("CANCELAR", new AbstractAction() {
			private static final long serialVersionUID = -4318457033998934089L;

			public void actionPerformed(ActionEvent e) {
				btnCancelar.doClick();
			}
		});
		
		/*
		 * Quando apetar F2 em qualquer lugar do contentPane executar o botão cadastrar
		 */
		contentPane.getInputMap(JPanel.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_F2, 0, true), "CADASTRAR");
		contentPane.getActionMap().put("CADASTRAR", new AbstractAction() {
			private static final long serialVersionUID = 2920756121779651720L;

			public void actionPerformed(ActionEvent e) {
				btnCadastrar.doClick();
			}
		});
		
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.CENTER);
		panel.setLayout(new BorderLayout(0, 0));

		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new TitledBorder(new LineBorder(new Color(184, 207,
				229)), " Dados Pessoais ", TitledBorder.LEADING,
				TitledBorder.TOP, null, null));
		panel.add(panel_2, BorderLayout.NORTH);

		JLabel lblPaciente = new JLabel("Paciente:");

		textFieldPaciente = new JTextField();

		JLabel lblSexo = new JLabel("Sexo:");

		comboBoxSexo = new JComboBox<Object>();
		comboBoxSexo.setModel(new DefaultComboBoxModel<Object>(new String[] {
				"Masculino", "Feminino" }));

		JLabel lblNascimento = new JLabel("Nascimento:");

		try {
			formattedTextFieldNascimento = new JFormattedTextField(new javax.swing.text.MaskFormatter("##/##/####"));
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			throw new IAnaliseException(e1.toString());
		}

		JLabel lblNaturalidade = new JLabel("Naturalidade:");

		comboBoxNaturalidade = new JComboBox<Object>();
		comboBoxNaturalidade.setModel(new DefaultComboBoxModel<Object>(new String[] {
				"", "AC", "AL", "AP", "AM", "BA", "CE", "DF", "ES", "GO", "MA",
				"MT", "MS", "MG", "PA", "PB", "PR", "PE", "PI", "RJ", "RN",
				"RS", "RO", "RR", "SC", "SP", "SE", "TO" }));

		JLabel lblEstadoCivil = new JLabel("Estado Civil:");

		comboBoxEstadoCivil = new JComboBox<Object>();
		comboBoxEstadoCivil.setModel(new DefaultComboBoxModel<Object>(new String[] {
				"", "Solteiro(a)", "Casado(a)", "Separado(a)", "Viúvo(a)" }));

		JLabel lblCpf = new JLabel("CPF:");

		try {
			formattedTextFieldCPF = new JFormattedTextField(new javax.swing.text.MaskFormatter("###.###.###-##"));
		} catch (ParseException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
			throw new IAnaliseException(e2.toString());
		}

		JLabel lblRg = new JLabel("RG:");

		textFieldRG = new JTextField();
		textFieldRG.setColumns(10);

		JLabel lblOrgoExpedidor = new JLabel("Orgão Expedidor:");

		textFieldOrgaoExpedidor = new JTextField();
		textFieldOrgaoExpedidor.setColumns(10);

		JLabel lblEstado = new JLabel("Estado:");

		comboBoxEstadoRG = new JComboBox<Object>();
		comboBoxEstadoRG.setModel(new DefaultComboBoxModel<Object>(new String[] { "",
				"AC", "AL", "AP", "AM", "BA", "CE", "DF", "ES", "GO", "MA",
				"MT", "MS", "MG", "PA", "PB", "PR", "PE", "PI", "RJ", "RN",
				"RS", "RO", "RR", "SC", "SP", "SE", "TO" }));

		JLabel lblEmisso = new JLabel("Emissão:");

		try {
			formattedTextFieldEmissaoRG = new JFormattedTextField(new javax.swing.text.MaskFormatter("##/##/####"));
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			throw new IAnaliseException(e1.toString());
		}

		JLabel lblNomeDoPai = new JLabel("Nome do Pai:");

		textFieldNomeDoPai = new JTextField();
		textFieldNomeDoPai.setColumns(10);

		JLabel lblNomeDaMe = new JLabel("Nome da Mãe:");

		textFieldNomeDaMae = new JTextField();
		textFieldNomeDaMae.setColumns(10);

		JLabel lblTelefone = new JLabel("Telefone:");

		try {
			formattedTextFieldTelefone = new JFormattedTextField(new javax.swing.text.MaskFormatter("(##)####-####"));
		} catch (ParseException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
			throw new IAnaliseException(e2.toString());
		}

		JLabel lblCelular = new JLabel("Celular:");

		try {
			formattedTextFieldCelular = new JFormattedTextField(new javax.swing.text.MaskFormatter("(##)####-####"));
		} catch (ParseException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
			throw new IAnaliseException(e2.toString());
		}

		JLabel lblEmail = new JLabel("E-mail:");

		textFieldEmail = new JTextField();
		textFieldEmail.setColumns(10);

		JLabel lblOrigem = new JLabel("Origem:");

		comboBoxOrigem = new JComboBox<Object>();
		comboBoxOrigem.setModel(new DefaultComboBoxModel<Object>(new String[] {
				"Interno", "Hemodiálise", "Eletivo" }));

		JLabel lblConvnio = new JLabel("Convênio:");

		textFieldConvenio = new JTextField();
		textFieldConvenio.setColumns(10);

		JLabel lblMdico = new JLabel("Médico:");

		textFieldMedico = new JTextField();
		textFieldMedico.setColumns(10);

		JLabel lblProfisso = new JLabel("Profissão:");

		textFieldProfissao = new JTextField();
		textFieldProfissao.setColumns(10);
		GroupLayout gl_panel_2 = new GroupLayout(panel_2);
		gl_panel_2
				.setHorizontalGroup(gl_panel_2
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_panel_2
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												gl_panel_2
														.createParallelGroup(
																Alignment.LEADING)
														.addGroup(
																gl_panel_2
																		.createSequentialGroup()
																		.addGroup(
																				gl_panel_2
																						.createParallelGroup(
																								Alignment.LEADING)
																						.addGroup(
																								gl_panel_2
																										.createSequentialGroup()
																										.addComponent(
																												lblPaciente)
																										.addPreferredGap(
																												ComponentPlacement.RELATED)
																										.addComponent(
																												textFieldPaciente,
																												GroupLayout.DEFAULT_SIZE,
																												284,
																												Short.MAX_VALUE)
																										.addPreferredGap(
																												ComponentPlacement.UNRELATED)
																										.addComponent(
																												lblSexo)
																										.addPreferredGap(
																												ComponentPlacement.RELATED)
																										.addComponent(
																												comboBoxSexo,
																												GroupLayout.PREFERRED_SIZE,
																												GroupLayout.DEFAULT_SIZE,
																												GroupLayout.PREFERRED_SIZE)
																										.addPreferredGap(
																												ComponentPlacement.UNRELATED)
																										.addComponent(
																												lblNascimento)
																										.addPreferredGap(
																												ComponentPlacement.RELATED)
																										.addComponent(
																												formattedTextFieldNascimento,
																												GroupLayout.PREFERRED_SIZE,
																												89,
																												GroupLayout.PREFERRED_SIZE)
																										.addGap(28))
																						.addGroup(
																								gl_panel_2
																										.createSequentialGroup()
																										.addComponent(
																												lblNaturalidade)
																										.addPreferredGap(
																												ComponentPlacement.RELATED)
																										.addComponent(
																												comboBoxNaturalidade,
																												GroupLayout.PREFERRED_SIZE,
																												GroupLayout.DEFAULT_SIZE,
																												GroupLayout.PREFERRED_SIZE)
																										.addPreferredGap(
																												ComponentPlacement.UNRELATED)
																										.addComponent(
																												lblEstadoCivil)
																										.addPreferredGap(
																												ComponentPlacement.RELATED)
																										.addComponent(
																												comboBoxEstadoCivil,
																												GroupLayout.PREFERRED_SIZE,
																												GroupLayout.DEFAULT_SIZE,
																												GroupLayout.PREFERRED_SIZE)
																										.addPreferredGap(
																												ComponentPlacement.UNRELATED)
																										.addComponent(
																												lblCpf)
																										.addPreferredGap(
																												ComponentPlacement.RELATED)
																										.addComponent(
																												formattedTextFieldCPF,
																												GroupLayout.PREFERRED_SIZE,
																												119,
																												GroupLayout.PREFERRED_SIZE))
																						.addGroup(
																								gl_panel_2
																										.createSequentialGroup()
																										.addComponent(
																												lblRg)
																										.addPreferredGap(
																												ComponentPlacement.RELATED)
																										.addComponent(
																												textFieldRG,
																												GroupLayout.PREFERRED_SIZE,
																												141,
																												GroupLayout.PREFERRED_SIZE)
																										.addPreferredGap(
																												ComponentPlacement.RELATED)
																										.addComponent(
																												lblOrgoExpedidor)
																										.addPreferredGap(
																												ComponentPlacement.RELATED)
																										.addComponent(
																												textFieldOrgaoExpedidor,
																												GroupLayout.PREFERRED_SIZE,
																												108,
																												GroupLayout.PREFERRED_SIZE)
																										.addPreferredGap(
																												ComponentPlacement.UNRELATED)
																										.addComponent(
																												lblEstado)
																										.addPreferredGap(
																												ComponentPlacement.RELATED)
																										.addComponent(
																												comboBoxEstadoRG,
																												GroupLayout.PREFERRED_SIZE,
																												GroupLayout.DEFAULT_SIZE,
																												GroupLayout.PREFERRED_SIZE)
																										.addPreferredGap(
																												ComponentPlacement.UNRELATED)
																										.addComponent(
																												lblEmisso)
																										.addPreferredGap(
																												ComponentPlacement.UNRELATED)
																										.addComponent(
																												formattedTextFieldEmissaoRG,
																												GroupLayout.PREFERRED_SIZE,
																												89,
																												GroupLayout.PREFERRED_SIZE)))
																		.addGap(0))
														.addGroup(
																gl_panel_2
																		.createSequentialGroup()
																		.addComponent(
																				lblNomeDoPai)
																		.addPreferredGap(
																				ComponentPlacement.RELATED)
																		.addComponent(
																				textFieldNomeDoPai,
																				GroupLayout.DEFAULT_SIZE,
																				260,
																				Short.MAX_VALUE)
																		.addPreferredGap(
																				ComponentPlacement.UNRELATED)
																		.addComponent(
																				lblNomeDaMe)
																		.addPreferredGap(
																				ComponentPlacement.RELATED)
																		.addComponent(
																				textFieldNomeDaMae,
																				GroupLayout.DEFAULT_SIZE,
																				258,
																				Short.MAX_VALUE)
																		.addContainerGap())
														.addGroup(
																gl_panel_2
																		.createSequentialGroup()
																		.addComponent(
																				lblTelefone)
																		.addPreferredGap(
																				ComponentPlacement.RELATED)
																		.addComponent(
																				formattedTextFieldTelefone,
																				GroupLayout.PREFERRED_SIZE,
																				120,
																				GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				ComponentPlacement.UNRELATED)
																		.addComponent(
																				lblCelular)
																		.addPreferredGap(
																				ComponentPlacement.RELATED)
																		.addComponent(
																				formattedTextFieldCelular,
																				GroupLayout.PREFERRED_SIZE,
																				120,
																				GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				ComponentPlacement.UNRELATED)
																		.addComponent(
																				lblEmail)
																		.addPreferredGap(
																				ComponentPlacement.RELATED)
																		.addComponent(
																				textFieldEmail,
																				GroupLayout.DEFAULT_SIZE,
																				273,
																				Short.MAX_VALUE)
																		.addContainerGap())
														.addGroup(
																gl_panel_2
																		.createSequentialGroup()
																		.addComponent(
																				lblOrigem)
																		.addPreferredGap(
																				ComponentPlacement.RELATED)
																		.addComponent(
																				comboBoxOrigem,
																				GroupLayout.PREFERRED_SIZE,
																				GroupLayout.DEFAULT_SIZE,
																				GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				ComponentPlacement.UNRELATED)
																		.addComponent(
																				lblConvnio)
																		.addPreferredGap(
																				ComponentPlacement.RELATED)
																		.addComponent(
																				textFieldConvenio,
																				GroupLayout.PREFERRED_SIZE,
																				GroupLayout.DEFAULT_SIZE,
																				GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				ComponentPlacement.UNRELATED)
																		.addComponent(
																				lblMdico)
																		.addPreferredGap(
																				ComponentPlacement.RELATED)
																		.addComponent(
																				textFieldMedico,
																				GroupLayout.DEFAULT_SIZE,
																				272,
																				Short.MAX_VALUE)
																		.addContainerGap())
														.addGroup(
																gl_panel_2
																		.createSequentialGroup()
																		.addComponent(
																				lblProfisso)
																		.addPreferredGap(
																				ComponentPlacement.RELATED)
																		.addComponent(
																				textFieldProfissao,
																				GroupLayout.PREFERRED_SIZE,
																				216,
																				GroupLayout.PREFERRED_SIZE)
																		.addContainerGap()))));
		gl_panel_2
				.setVerticalGroup(gl_panel_2
						.createParallelGroup(Alignment.TRAILING)
						.addGroup(
								gl_panel_2
										.createSequentialGroup()
										.addContainerGap(
												GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)
										.addGroup(
												gl_panel_2
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																lblPaciente)
														.addComponent(
																textFieldPaciente,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(lblSexo)
														.addComponent(
																comboBoxSexo,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(
																lblNascimento)
														.addComponent(
																formattedTextFieldNascimento,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												ComponentPlacement.RELATED)
										.addGroup(
												gl_panel_2
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																lblNaturalidade)
														.addComponent(
																comboBoxNaturalidade,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(
																lblEstadoCivil)
														.addComponent(
																comboBoxEstadoCivil,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(lblCpf)
														.addComponent(
																formattedTextFieldCPF,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												ComponentPlacement.RELATED)
										.addGroup(
												gl_panel_2
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(lblRg)
														.addComponent(
																textFieldRG,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(
																lblOrgoExpedidor)
														.addComponent(
																textFieldOrgaoExpedidor,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(
																formattedTextFieldEmissaoRG,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(lblEmisso)
														.addComponent(
																comboBoxEstadoRG,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(lblEstado))
										.addPreferredGap(
												ComponentPlacement.RELATED)
										.addGroup(
												gl_panel_2
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																lblNomeDoPai)
														.addComponent(
																textFieldNomeDoPai,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(
																lblNomeDaMe)
														.addComponent(
																textFieldNomeDaMae,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												ComponentPlacement.RELATED)
										.addGroup(
												gl_panel_2
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																lblTelefone)
														.addComponent(
																formattedTextFieldTelefone,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(
																lblCelular)
														.addComponent(
																formattedTextFieldCelular,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(lblEmail)
														.addComponent(
																textFieldEmail,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												ComponentPlacement.RELATED)
										.addGroup(
												gl_panel_2
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(lblOrigem)
														.addComponent(
																comboBoxOrigem,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(
																lblConvnio)
														.addComponent(
																textFieldConvenio,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(lblMdico)
														.addComponent(
																textFieldMedico,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												ComponentPlacement.RELATED)
										.addGroup(
												gl_panel_2
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																lblProfisso)
														.addComponent(
																textFieldProfissao,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addContainerGap()));
		panel_2.setLayout(gl_panel_2);

		JPanel panel_3 = new JPanel();
		panel_3.setBorder(new TitledBorder(new LineBorder(new Color(184, 207,
				229)), " Endere\u00E7o ", TitledBorder.LEADING,
				TitledBorder.TOP, null, null));
		panel.add(panel_3, BorderLayout.SOUTH);

		JLabel lblRua = new JLabel("Rua:");

		textFieldRua = new JTextField();
		textFieldRua.setColumns(10);

		JLabel lblNmero = new JLabel("Número:");

		textFieldNumero = new JTextField();
		textFieldNumero.setColumns(10);

		JLabel lblBairro = new JLabel("Bairro:");

		textFieldBairro = new JTextField();
		textFieldBairro.setColumns(10);

		JLabel lblCidade = new JLabel("Cidade:");

		textFieldCidade = new JTextField();
		textFieldCidade.setColumns(10);

		JLabel lblEstado_1 = new JLabel("Estado:");

		comboBoxEstadoEndereco = new JComboBox<Object>();
		comboBoxEstadoEndereco.setModel(new DefaultComboBoxModel<Object>(new String[] {
				"", "AC", "AL", "AP", "AM", "BA", "CE", "DF", "ES", "GO", "MA",
				"MT", "MS", "MG", "PA", "PB", "PR", "PE", "PI", "RJ", "RN",
				"RS", "RO", "RR", "SC", "SP", "SE", "TO" }));

		JLabel lblComplemento = new JLabel("Complemento:");

		textFieldComplemento = new JTextField();
		textFieldComplemento.setColumns(10);

		JLabel lblCep = new JLabel("CEP:");

		try {
			formattedTextFieldCEP = new JFormattedTextField(new javax.swing.text.MaskFormatter("#####-###"));
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			throw new IAnaliseException(e1.toString());
		}
		GroupLayout gl_panel_3 = new GroupLayout(panel_3);
		gl_panel_3
				.setHorizontalGroup(gl_panel_3
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_panel_3
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												gl_panel_3
														.createParallelGroup(
																Alignment.LEADING)
														.addGroup(
																gl_panel_3
																		.createSequentialGroup()
																		.addComponent(
																				lblRua)
																		.addGap(12)
																		.addComponent(
																				textFieldRua,
																				GroupLayout.PREFERRED_SIZE,
																				176,
																				GroupLayout.PREFERRED_SIZE)
																		.addGap(18)
																		.addComponent(
																				lblNmero)
																		.addGap(12)
																		.addComponent(
																				textFieldNumero,
																				GroupLayout.PREFERRED_SIZE,
																				45,
																				GroupLayout.PREFERRED_SIZE)
																		.addGap(18)
																		.addComponent(
																				lblBairro)
																		.addGap(12)
																		.addComponent(
																				textFieldBairro,
																				GroupLayout.PREFERRED_SIZE,
																				GroupLayout.DEFAULT_SIZE,
																				GroupLayout.PREFERRED_SIZE)
																		.addGap(18)
																		.addComponent(
																				lblCidade)
																		.addPreferredGap(
																				ComponentPlacement.RELATED)
																		.addComponent(
																				textFieldCidade,
																				GroupLayout.DEFAULT_SIZE,
																				123,
																				Short.MAX_VALUE))
														.addGroup(
																gl_panel_3
																		.createSequentialGroup()
																		.addComponent(
																				lblEstado_1)
																		.addGap(12)
																		.addComponent(
																				comboBoxEstadoEndereco,
																				GroupLayout.PREFERRED_SIZE,
																				GroupLayout.DEFAULT_SIZE,
																				GroupLayout.PREFERRED_SIZE)
																		.addGap(18)
																		.addComponent(
																				lblComplemento)
																		.addGap(12)
																		.addComponent(
																				textFieldComplemento,
																				GroupLayout.PREFERRED_SIZE,
																				GroupLayout.DEFAULT_SIZE,
																				GroupLayout.PREFERRED_SIZE)
																		.addGap(18)
																		.addComponent(
																				lblCep)
																		.addGap(12)
																		.addComponent(
																				formattedTextFieldCEP,
																				GroupLayout.PREFERRED_SIZE,
																				92,
																				GroupLayout.PREFERRED_SIZE)))
										.addContainerGap()));
		gl_panel_3
				.setVerticalGroup(gl_panel_3
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_panel_3
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												gl_panel_3
														.createParallelGroup(
																Alignment.LEADING)
														.addComponent(
																textFieldCidade,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.addGroup(
																gl_panel_3
																		.createSequentialGroup()
																		.addGroup(
																				gl_panel_3
																						.createParallelGroup(
																								Alignment.LEADING)
																						.addComponent(
																								lblRua)
																						.addComponent(
																								textFieldRua,
																								GroupLayout.PREFERRED_SIZE,
																								GroupLayout.DEFAULT_SIZE,
																								GroupLayout.PREFERRED_SIZE)
																						.addGroup(
																								gl_panel_3
																										.createSequentialGroup()
																										.addGap(2)
																										.addComponent(
																												lblNmero))
																						.addComponent(
																								textFieldNumero,
																								GroupLayout.PREFERRED_SIZE,
																								GroupLayout.DEFAULT_SIZE,
																								GroupLayout.PREFERRED_SIZE)
																						.addGroup(
																								gl_panel_3
																										.createSequentialGroup()
																										.addGap(2)
																										.addComponent(
																												lblBairro))
																						.addComponent(
																								textFieldBairro,
																								GroupLayout.PREFERRED_SIZE,
																								GroupLayout.DEFAULT_SIZE,
																								GroupLayout.PREFERRED_SIZE)
																						.addGroup(
																								gl_panel_3
																										.createSequentialGroup()
																										.addGap(2)
																										.addComponent(
																												lblCidade)))
																		.addGap(6)
																		.addGroup(
																				gl_panel_3
																						.createParallelGroup(
																								Alignment.LEADING)
																						.addGroup(
																								gl_panel_3
																										.createSequentialGroup()
																										.addGap(5)
																										.addComponent(
																												lblEstado_1))
																						.addComponent(
																								comboBoxEstadoEndereco,
																								GroupLayout.PREFERRED_SIZE,
																								GroupLayout.DEFAULT_SIZE,
																								GroupLayout.PREFERRED_SIZE)
																						.addGroup(
																								gl_panel_3
																										.createSequentialGroup()
																										.addGap(5)
																										.addComponent(
																												lblComplemento))
																						.addGroup(
																								gl_panel_3
																										.createSequentialGroup()
																										.addGap(3)
																										.addComponent(
																												textFieldComplemento,
																												GroupLayout.PREFERRED_SIZE,
																												GroupLayout.DEFAULT_SIZE,
																												GroupLayout.PREFERRED_SIZE))
																						.addGroup(
																								gl_panel_3
																										.createSequentialGroup()
																										.addGap(5)
																										.addComponent(
																												lblCep))
																						.addGroup(
																								gl_panel_3
																										.createSequentialGroup()
																										.addGap(3)
																										.addComponent(
																												formattedTextFieldCEP,
																												GroupLayout.PREFERRED_SIZE,
																												GroupLayout.DEFAULT_SIZE,
																												GroupLayout.PREFERRED_SIZE)))))
										.addContainerGap(
												GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)));
		panel_3.setLayout(gl_panel_3);

		JPanel panel_4 = new JPanel();
		panel_4.setBorder(new TitledBorder(new LineBorder(new Color(184, 207,
				229)), " Observa\u00E7\u00F5es ", TitledBorder.LEADING,
				TitledBorder.TOP, null, null));
		panel.add(panel_4, BorderLayout.CENTER);
		panel_4.setLayout(new BorderLayout(0, 0));

		JScrollPane scrollPane = new JScrollPane();
		panel_4.add(scrollPane, BorderLayout.CENTER);

		textAreaObservacoes = new JTextArea();
		textAreaObservacoes.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
				contReleased = 0;
			}

			@Override
			public void focusLost(FocusEvent e) {
				contReleased = 0;
			}
		});
		textAreaObservacoes.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_TAB) {
					if (contReleased > 0)
						textFieldRua.requestFocus();
					textAreaObservacoes.setText(tempTextAreaObservacoes);
				}
				tempTextAreaObservacoes = textAreaObservacoes.getText()
						.toString();
				contReleased++;
			}
		});
		scrollPane.setViewportView(textAreaObservacoes);
		textAreaObservacoes.setLineWrap(true);

		JPanel panel_1 = new JPanel();
		contentPane.add(panel_1, BorderLayout.SOUTH);

		btnLimparFormulrio = new JButton("Limpar Formulário");
		btnLimparFormulrio.setMnemonic(76);
		btnLimparFormulrio.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				/*System.out.println("textFieldPaciente " + textFieldPaciente.getText());
				System.out.println("comboBoxSexo " + comboBoxSexo.getSelectedItem().toString());
				System.out.println("formattedTextFieldNascimento " + formattedTextFieldNascimento.getText());
				System.out.println("comboBoxNaturalidade " + comboBoxNaturalidade.getSelectedItem().toString());
				System.out.println("comboBoxEstadoCivil " + comboBoxEstadoCivil.getSelectedItem().toString());
				System.out.println("formattedTextFieldCPF " + formattedTextFieldCPF.getText());
				System.out.println("textFieldRG " + textFieldRG.getText());
				System.out.println("textFieldOrgaoExpedidor " + textFieldOrgaoExpedidor.getText());
				System.out.println("comboBoxEstadoRG " + comboBoxEstadoRG.getSelectedItem().toString());
				System.out.println("formattedTextFieldEmissaoRG " + formattedTextFieldEmissaoRG.getText());
				System.out.println("textFieldNomeDoPai " + textFieldNomeDoPai.getText());
				System.out.println("textFieldNomeDaMae " + textFieldNomeDaMae.getText());
				System.out.println("formattedTextFieldTelefone " + formattedTextFieldTelefone.getText());
				System.out.println("formattedTextFieldCelular " + formattedTextFieldCelular.getText());
				System.out.println("textFieldEmail " + textFieldEmail.getText());
				System.out.println("comboBoxOrigem " + comboBoxOrigem.getSelectedItem().toString());
				System.out.println("textFieldConvenio " + textFieldConvenio.getText());
				System.out.println("textFieldMedico " + textFieldMedico.getText());
				System.out.println("textFieldProfissao " + textFieldProfissao.getText());
				System.out.println("textAreaObservacoes " + textAreaObservacoes.getText());
				System.out.println("textFieldRua " + textFieldRua.getText());
				System.out.println("textFieldNumero " + textFieldNumero.getText());
				System.out.println("textFieldBairro " + textFieldBairro.getText());
				System.out.println("textFieldCidade " + textFieldCidade.getText());
				System.out.println("comboBoxEstadoEndereco " + comboBoxEstadoEndereco.getSelectedItem().toString());
				System.out.println("textFieldComplemento " + textFieldComplemento.getText());
				System.out.println("formattedTextFieldCEP " + formattedTextFieldCEP.getText());*/
				int opcao = JOptionPane.showOptionDialog(null,
                        "Deseja realmente limpar o formulário?",
                        titulo, JOptionPane.YES_NO_OPTION,
                        JOptionPane.QUESTION_MESSAGE, null,
                        null, null);
				
				if (opcao == 0) { // se desejar cadastrar
					textFieldPaciente.setText("");
					comboBoxSexo.setSelectedIndex(0);
					formattedTextFieldNascimento.setText("");
					comboBoxNaturalidade.setSelectedIndex(0);
					comboBoxEstadoCivil.setSelectedIndex(0);
					formattedTextFieldCPF.setText("");
					textFieldRG.setText("");
					textFieldOrgaoExpedidor.setText("");
					comboBoxEstadoRG.setSelectedIndex(0);
					formattedTextFieldEmissaoRG.setText("");
					textFieldNomeDoPai.setText("");
					textFieldNomeDaMae.setText("");
					formattedTextFieldTelefone.setText("");
					formattedTextFieldCelular.setText("");
					textFieldEmail.setText("");
					comboBoxOrigem.setSelectedIndex(0);
					textFieldConvenio.setText("");
					textFieldMedico.setText("");
					textFieldProfissao.setText("");
					textAreaObservacoes.setText("");
					textFieldRua.setText("");
					textFieldNumero.setText("");
					textFieldBairro.setText("");
					textFieldCidade.setText("");
					comboBoxEstadoEndereco.setSelectedIndex(0);
					textFieldComplemento.setText("");
					formattedTextFieldCEP.setText("");
				}
			}
		});
		panel_1.add(btnLimparFormulrio);

		btnCancelar = new JButton("Cancelar (ESC)");
		btnCancelar.setMnemonic('a');
		btnCancelar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int opcao = JOptionPane.showOptionDialog(null,
                        "Deseja realmente cancelar este cadastro?",
                        titulo, JOptionPane.YES_NO_OPTION,
                        JOptionPane.QUESTION_MESSAGE, null,
                        null, null);
				
				if (opcao == 0) { // se desejar cadastrar
					dispose();
				}
			}
		});
		panel_1.add(btnCancelar);

		btnCadastrar = new JButton("Cadastrar (F2)");
		btnCadastrar.setMnemonic(67);
		btnCadastrar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int opcao = JOptionPane.showOptionDialog(null,
                        "Deseja realmente realizar este cadastro?",
                        titulo, JOptionPane.YES_NO_OPTION,
                        JOptionPane.QUESTION_MESSAGE, null,
                        null, null);
				
				boolean cadastrar = true;
				if (opcao == 0) { // se desejar cadastrar
					if(textFieldPaciente.getText().equals("")) {
						cadastrar = false;
						JOptionPane.showMessageDialog(
                                        null,
                                        "Nome do paciente não foi informado!",
                                        titulo+" - Alerta",
                                        JOptionPane.WARNING_MESSAGE,
                                        null);
						textFieldPaciente.requestFocus();
					} else if(!formattedTextFieldNascimento.getText().equals("  /  /    ")) {
						try {
							if(!Validacoes.isDate(formattedTextFieldNascimento.getText())) {
								cadastrar = false;
								JOptionPane.showMessageDialog(
							                    null,
							                    "A data de nascimento não é válida!",
							                    titulo+" - Alerta",
							                    JOptionPane.WARNING_MESSAGE,
							                    null);
								formattedTextFieldNascimento.requestFocus();
							}
						} catch (HeadlessException e1) {
							cadastrar = false;
							// TODO Auto-generated catch block
							JOptionPane.showMessageDialog(
				                    null,
				                    "Ocorreu um erro inesperado!\nInforme ao desenvolvedor do sistema.\n\nErro retornado:\n("+e1.toString()+")",
				                    titulo+" - Erro",
				                    JOptionPane.ERROR_MESSAGE,
				                    null);
						} catch (ParseException e1) {
							cadastrar = false;
							// TODO Auto-generated catch block
							JOptionPane.showMessageDialog(
				                    null,
				                    "Ocorreu um erro inesperado!\nInforme ao desenvolvedor do sistema.\n\nErro retornado:\n("+e1.toString()+")",
				                    titulo+" - Erro",
				                    JOptionPane.ERROR_MESSAGE,
				                    null);
						}
					} else if(!formattedTextFieldCPF.getText().equals("   .   .   -  ")) {
						String tempCPF = formattedTextFieldCPF.getText().substring(0, 3)+formattedTextFieldCPF.getText().substring(4, 7)+formattedTextFieldCPF.getText().substring(8, 11)+formattedTextFieldCPF.getText().substring(12, 14);
						if(!Validacoes.isCPF(tempCPF)) {
							cadastrar = false;
							JOptionPane.showMessageDialog(
						                    null,
						                    "O CPF não é válido!",
						                    titulo+" - Alerta",
						                    JOptionPane.WARNING_MESSAGE,
						                    null);
							formattedTextFieldCPF.requestFocus();
						}
					} else if(!formattedTextFieldEmissaoRG.getText().equals("  /  /    ")) {
						try {
							if(!Validacoes.isDate(formattedTextFieldEmissaoRG.getText())) {
								cadastrar = false;
								JOptionPane.showMessageDialog(
							                    null,
							                    "A data de emissão do RG não é válida!",
							                    titulo+" - Alerta",
							                    JOptionPane.WARNING_MESSAGE,
							                    null);
								formattedTextFieldEmissaoRG.requestFocus();
							}
						} catch (HeadlessException e1) {
							cadastrar = false;
							// TODO Auto-generated catch block
							JOptionPane.showMessageDialog(
				                    null,
				                    "Ocorreu um erro inesperado!\nInforme ao desenvolvedor do sistema.\n\nErro retornado:\n("+e1.toString()+")",
				                    titulo+" - Erro",
				                    JOptionPane.ERROR_MESSAGE,
				                    null);
						} catch (ParseException e1) {
							cadastrar = false;
							// TODO Auto-generated catch block
							JOptionPane.showMessageDialog(
				                    null,
				                    "Ocorreu um erro inesperado!\nInforme ao desenvolvedor do sistema.\n\nErro retornado:\n("+e1.toString()+")",
				                    titulo+" - Erro",
				                    JOptionPane.ERROR_MESSAGE,
				                    null);
						}
					} else if(!textFieldEmail.getText().equals("")) {
						if(!Validacoes.isEmail(textFieldEmail.getText())) {
							cadastrar = false;
							JOptionPane.showMessageDialog(
						                    null,
						                    "O e-mail não é válido!",
						                    titulo+" - Alerta",
						                    JOptionPane.WARNING_MESSAGE,
						                    null);
							textFieldEmail.requestFocus();
						}
					}
					
					if(cadastrar == true) {
						//aqui finalmente é cadastrado
						Paciente paciente = new Paciente();
						paciente.setPaciente(textFieldPaciente.getText());
						paciente.setSexo(comboBoxSexo.getSelectedItem().toString());

						if(formattedTextFieldNascimento.getText().equals("  /  /    ")) {
							paciente.setNascimento("");
						} else {
							paciente.setNascimento(formattedTextFieldNascimento.getText());
						}
						
						paciente.setNaturalidade(comboBoxNaturalidade.getSelectedItem().toString());
						paciente.setEstadoCivil(comboBoxEstadoCivil.getSelectedItem().toString());
						
						if(formattedTextFieldCPF.getText().equals("   .   .   -  ")) {
							paciente.setCpf("");
						} else {
							paciente.setCpf(formattedTextFieldCPF.getText());
						}
						
						paciente.setRg(textFieldRG.getText());
						paciente.setOrgaoExpedidor(textFieldOrgaoExpedidor.getText());
						paciente.setEstadoRG(comboBoxEstadoRG.getSelectedItem().toString());
						
						if(formattedTextFieldEmissaoRG.getText().equals("  /  /    ")) {
							paciente.setEmissaoRG("");
						} else {
							paciente.setEmissaoRG(formattedTextFieldEmissaoRG.getText());
						}
						
						paciente.setNomeDoPai(textFieldNomeDoPai.getText());
						paciente.setNomeDaMae(textFieldNomeDaMae.getText());
						
						if(formattedTextFieldTelefone.getText().equals("(  )    -    ")) {
							paciente.setTelefone("");
						} else {
							paciente.setTelefone(formattedTextFieldTelefone.getText());
						}
						
						
						if(formattedTextFieldCelular.getText().equals("(  )    -    ")) {
							paciente.setCelular("");
						} else {
							paciente.setCelular(formattedTextFieldCelular.getText());
						}
						
						paciente.setEmail(textFieldEmail.getText());
						paciente.setOrigem(comboBoxOrigem.getSelectedItem().toString());
						paciente.setConvenio(textFieldConvenio.getText());
						paciente.setMedico(textFieldMedico.getText());
						paciente.setProfissao(textFieldProfissao.getText());
						paciente.setObservacoes(textAreaObservacoes.getText());
						paciente.setRua(textFieldRua.getText());
						paciente.setNumero(textFieldNumero.getText());
						paciente.setBairro(textFieldBairro.getText());
						paciente.setCidade(textFieldCidade.getText());
						paciente.setEstadoEndereco(comboBoxEstadoEndereco.getSelectedItem().toString());
						paciente.setComplemento(textFieldComplemento.getText());
						
						if(formattedTextFieldCEP.getText().equals("     -   ")) {
							paciente.setCep("");
						} else {
							paciente.setCep(formattedTextFieldCEP.getText());
						}
						
						DateFormat dat = DateFormat.getDateInstance(DateFormat.DEFAULT, new Locale("pt","BR"));  
						String dataCadastro = dat.format(new Date());
						paciente.setDataCadastro(dataCadastro);
						
						boolean cadastrou = false;
						try {
							cadastrou = Fachada.getInstancia().cadastrarPaciente(paciente);
						} catch (Exception e1) {
							cadastrou = false;
							e1.printStackTrace();
							try {
								throw new IAnaliseException(e.toString());
							} catch (IAnaliseException e2) {
								// TODO Auto-generated catch block
								e2.printStackTrace();
							}
						}
						
						if(cadastrou) {
							JOptionPane.showMessageDialog(
				                    null,
				                    "O cadastro foi realizado com sucesso!",
				                    titulo+" - Informação",
				                    JOptionPane.INFORMATION_MESSAGE,
				                    null);
						} else {
							JOptionPane.showMessageDialog(
				                    null,
				                    "O cadastro não foi realizado!",
				                    titulo+" - Erro",
				                    JOptionPane.ERROR_MESSAGE,
				                    null);
						}
					}
					
				}

			}
		});
		panel_1.add(btnCadastrar);
	}
}
